﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form3 : Form
    {
        string year;
        string month;
        string day;
        string strSQL;
        public Form3()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            time();
            string strSQL = "select * from classroom where used='null' ;";
            //MessageBox.Show(strSQL);
            SqlConnection connection = new SqlConnection(SqlHelper.Constr);
            DataTable stuDT = new DataTable();
            SqlDataAdapter sqd = new SqlDataAdapter(strSQL, connection);
            DataTable dataTable = new DataTable();
            sqd.Fill(dataTable);
            stuDT = dataTable;
            this.dataGridView1.DataSource = stuDT;
        }
        public void time()
        {
            DateTime time = DateTime.Now;
            year = time.Year.ToString();
            month = time.Month.ToString();
            if (month.Length == 1)
            {
                month = '0' + month;
            }
            day = time.Day.ToString();
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            string classroomid = textBox1.Text;
            string dt = dateTimePicker1.Text;
            var query = "select * from classroom where classroomid=" + classroomid + " and [date]='" + dt + "';";
            SqlConnection connection = new SqlConnection(SqlHelper.Constr);
            connection.Open();
            SqlDataAdapter dataAdapter = new SqlDataAdapter(query, connection);
            DataTable dataTable = new DataTable();
            dataAdapter.Fill(dataTable);
            
            this.dataGridView1.DataSource = dataTable;

        }

        private void button3_Click(object sender, EventArgs e)
        {
            string classroomid = textBox1.Text;
            string dt = dateTimePicker1.Text;
            string userid = textBox2.Text;
            string section = comboBox1.Text;
            string used = comboBox2.Text;
            var query = "update borrow set borrow_date='" + dt + "', borrow_section='" + section + "', userid=" + userid + ", classroomid=" + classroomid + ";";
            SqlConnection connection = new SqlConnection(SqlHelper.Constr);
            connection.Open();
            SqlCommand command = new SqlCommand(query, connection);
            command.ExecuteNonQuery();
            var query1 = "select * from borrow where classroomid="+classroomid+";";
            SqlDataAdapter dataAdapter = new SqlDataAdapter(query1, connection);
            DataTable dataTable = new DataTable();
            dataAdapter.Fill(dataTable);
            this.dataGridView1.DataSource = dataTable;

        }
    }
}
